/*
 * @lc app=leetcode.cn id=43 lang=typescript
 *
 * [43] 字符串相乘
 */

// @lc code=start
function multiply(num1: string, num2: string): string {
    const size = num1.length + num2.length;
    const res = new Array(size).fill(0);
    for (let i = num2.length - 1; i >= 0; i--) {
        for (let j = num1.length - 1; j >= 0; j--) {
            const pro = Number(num1[j]) * Number(num2[i])
            res[i + j + 1] += pro;
        }
    }
    console.log(res)
    let w = 0;
    for (let i = res.length - 1; i >= 0; i--) {

        if (res[i] > 9) {
            w += Math.floor(res[i] / 10);
            res[i] = res[i] % 10;
        }
        if (res[i - 1] !== undefined) {
            res[i - 1] += w;
            w = 0;
        }
    }
    let k = 0;
    while (k < size) {
        if (res[k] === 0) {
            res.shift();
        } else {
            break;
        }
    }
    if (res.length === 0) {
        res.push(0)
    }
    return res.join('');
};

// @lc code=end

